﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using efuture.utils;
using efuture.http;
using efuture.xml;
using System.Xml.XPath;
using System.IO;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid;

namespace efuture.vdrp
{
    public partial class frmMain : DevExpress.XtraEditors.XtraForm, IPlugin
    {
        private String ReportId = "", ModelName = "", Contents = "";

        public frmMain()
        {
            InitializeComponent();
        }

        #region IPlugin 成员

        public void afterPropertiesSet(Dictionary<string, string> properties)
        {
            HttpHelper.ServerURL = properties["ServerURL"];
            ReportId = properties["ReportId"];
        }

        public void shutdown()
        {
            _ctx.OnEvent(EventType.OnClosed);
        }

        private IPluginContext _ctx;
        public StartupResult startup(IPluginContext ctx)
        {
            _ctx = ctx;

            try
            {
                String xmlData = HttpHelper.DoGet(String.Format("/rest/report/custom/get?id={0}", ReportId), null);

                ModelName = XMLHelper.SelectSingleValue(xmlData, "/Root/Report/DataModel/ModelName");
                Contents = XMLHelper.SelectSingleValue(xmlData, "/Root/Report/Contents");

                PivotGridRender.Render(this.pivotGridControl1, Contents);
                PivotGridRender.Apply(this.pivotGridControl1, Contents);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return StartupResult.Failed;
            }
            return StartupResult.Success;
        }

        #endregion

        private void btnClose_Click(object sender, EventArgs e)
        {
            filter = null;
            shutdown();
        }

        private frmReportFilter filter = null;
        private void btnFilter_Click(object sender, EventArgs e)
        {
            if (filter == null) 
            {
                filter = new frmReportFilter();
            }

            filter.ReportId = ReportId;
            filter.DataModel = ModelName;
            filter.Fields = frmReportFilter.GetFields(this.pivotGridControl1);
            if (filter.ShowDialog() == DialogResult.OK)
            {
                this.pivotGridControl1.DataSource = null;

                String xmlData = HttpHelper.DoPost("/rest/report/custom/query", filter.FilterString, null);
                DataSet ds = new DataSet();
                ds.ReadXmlSchema(Application.StartupPath + "\\" + ModelName + ".xsd");
                ds.ReadXml(new StringReader(xmlData));
                this.pivotGridControl1.DataSource = ds.Tables[0];
            }
        }

        private void btnChart_Click(object sender, EventArgs e)
        {
            frmReportChart frm = new frmReportChart();
            frm.dsPGC = this.pivotGridControl1;
            frm.ShowDialog();
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            String FileName = ShowSaveFileDialog("Microsoft Excel 2007 Document", "Microsoft Excel|*.xlsx");
            if (!string.IsNullOrEmpty(FileName))
            {
                this.pivotGridControl1.ExportToXlsx(FileName);
            }
        }

        string ShowSaveFileDialog(string title, string filter)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Title = "Export To " + title;
            dlg.Filter = filter;
            if (dlg.ShowDialog() == DialogResult.OK) return dlg.FileName;
            return String.Empty;
        }
    }
}